<template>
    <div>
        <Card shadow>
            <div class="search-con search-con-top">
                <ButtonGroup>
                    <Button :disabled="hasAuthority('OrderRefundEdit}')?false:true"  class="search-btn" type="primary" @click="handleModal()">
                        <span>添加</span>
                    </Button>
                </ButtonGroup>
            </div>
            <Table :columns="columns" :data="data" :loading="loading">
                <template slot="action" slot-scope="{ row }">
                    <a :disabled="hasAuthority('OrderRefundEdit')?false:true"   @click="handleModal(row)">
                        编辑</a>&nbsp;
                    <Dropdown v-show="hasAuthority('OrderRefundEdit')" transfer ref="dropdown" @on-click="handleClick($event,row)">
                        <a href="javascript:void(0)">
                            <span>更多</span>
                            <Icon type="ios-arrow-down"></Icon>
                        </a>
                        <DropdownMenu slot="list">
                            <DropdownItem  name="remove">删除</DropdownItem>
                        </DropdownMenu>
                    </Dropdown>&nbsp;
                </template>
            </Table>
            <Page :total="pageInfo.total" :current="pageInfo.page" :page-size="pageInfo.limit" show-elevator show-sizer
                  show-total
                  @on-change="handlePage" @on-page-size-change='handlePageSize'></Page>
        </Card>
        <Modal v-model="modalVisible"
               :title="modalTitle"
               width="40"
               @on-cancel="handleReset">
            <div>
                <Form ref="form" :model="formItem" :rules="formItemRules" :label-width="100">
                    <FormItem label="订单id" prop="orderId">
                        <Input v-model="formItem.orderId"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="退款类型：1用户发起退款，2商户发起退款" prop="type">
                        <Input v-model="formItem.type"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="收货状态，0未收货1已收货" prop="inHand">
                        <Input v-model="formItem.inHand"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="退货退款原因" prop="reason">
                        <Input v-model="formItem.reason"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="状态，0没有退成功1退款审核中2等待退货3退货中4已退货退款" prop="status">
                        <Input v-model="formItem.status"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="退货退款说明" prop="explainn">
                        <Input v-model="formItem.explainn"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="收货人名称" prop="receiver">
                        <Input v-model="formItem.receiver"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="收货地址" prop="address">
                        <Input v-model="formItem.address"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="收货人联系号码" prop="phone">
                        <Input v-model="formItem.phone"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="物流公司" prop="logistics">
                        <Input v-model="formItem.logistics"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="快递单号" prop="courierNumber">
                        <Input v-model="formItem.courierNumber"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="寄出人联系号码" prop="senderPhone">
                        <Input v-model="formItem.senderPhone"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="申请退货退款失败原因" prop="refuseReason">
                        <Input v-model="formItem.refuseReason"  placeholder="请输入内容"></Input>
                    </FormItem>
                </Form>
                <div class="drawer-footer">
                    <Button type="default" @click="handleReset">取消</Button>&nbsp;
                    <Button type="primary" @click="handleSubmit" :loading="saving">保存</Button>
                </div>
            </div>
        </Modal>
    </div>
</template>

<script>
    import OrderRefund from '@/api/OrderRefund'

    export default {
        name: 'OrderRefund',
        data () {
            return {
                loading: false,
                saving: false,
                modalVisible: false,
                modalTitle: '',
                pageInfo: {
                    total: 0,
                    page: 1,
                    limit: 10
                },
                formItemRules: {
                    orderId: [
                        {required: true, message: '订单id不能为空', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '退款类型：1用户发起退款，2商户发起退款不能为空', trigger: 'blur'}
                    ],
                    inHand: [
                        {required: true, message: '收货状态，0未收货1已收货不能为空', trigger: 'blur'}
                    ],
                    reason: [
                        {required: true, message: '退货退款原因不能为空', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '状态，0没有退成功1退款审核中2等待退货3退货中4已退货退款不能为空', trigger: 'blur'}
                    ],
                    explainn: [
                        {required: true, message: '退货退款说明不能为空', trigger: 'blur'}
                    ],
                    receiver: [
                        {required: true, message: '收货人名称不能为空', trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: '收货地址不能为空', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '收货人联系号码不能为空', trigger: 'blur'}
                    ],
                    logistics: [
                        {required: true, message: '物流公司不能为空', trigger: 'blur'}
                    ],
                    courierNumber: [
                        {required: true, message: '快递单号不能为空', trigger: 'blur'}
                    ],
                    senderPhone: [
                        {required: true, message: '寄出人联系号码不能为空', trigger: 'blur'}
                    ],
                    refuseReason: [
                        {required: true, message: '申请退货退款失败原因不能为空', trigger: 'blur'}
                    ],
                },
                formItem: {
                    orderRefundId: '',
                    orderId: '',
                    type: '',
                    inHand: '',
                    reason: '',
                    status: '',
                    explainn: '',
                    receiver: '',
                    address: '',
                    phone: '',
                    logistics: '',
                    courierNumber: '',
                    senderPhone: '',
                    refuseReason: '',
                },
                columns: [
                    {
                        title: '订单id',
                        key: 'orderId',
                        width: 100
                    },
                    {
                        title: '退款类型：1用户发起退款，2商户发起退款',
                        key: 'type',
                        width: 100
                    },
                    {
                        title: '收货状态，0未收货1已收货',
                        key: 'inHand',
                        width: 100
                    },
                    {
                        title: '退货退款原因',
                        key: 'reason',
                        width: 100
                    },
                    {
                        title: '状态，0没有退成功1退款审核中2等待退货3退货中4已退货退款',
                        key: 'status',
                        width: 100
                    },
                    {
                        title: '退货退款说明',
                        key: 'explainn',
                        width: 100
                    },
                    {
                        title: '收货人名称',
                        key: 'receiver',
                        width: 100
                    },
                    {
                        title: '收货地址',
                        key: 'address',
                        width: 100
                    },
                    {
                        title: '收货人联系号码',
                        key: 'phone',
                        width: 100
                    },
                    {
                        title: '物流公司',
                        key: 'logistics',
                        width: 100
                    },
                    {
                        title: '快递单号',
                        key: 'courierNumber',
                        width: 100
                    },
                    {
                        title: '寄出人联系号码',
                        key: 'senderPhone',
                        width: 100
                    },
                    {
                        title: '申请退货退款失败原因',
                        key: 'refuseReason',
                        width: 100
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        fixed: 'right',
                        width: 120
                    }
                ],
                data: []
            }
        },
        methods: {
            handleModal (data) {
                if (data) {
                    this.modalTitle = '编辑订单退款信息'
                    this.formItem = Object.assign({}, this.formItem, data)
                } else {
                    this.modalTitle = '添加订单退款信息'
                }
                this.modalVisible = true
            },
            handleReset () {
                const newData = {
                    orderRefundId: '',
                    orderId: '',
                    type: '',
                    inHand: '',
                    reason: '',
                    status: '',
                    explainn: '',
                    receiver: '',
                    address: '',
                    phone: '',
                    logistics: '',
                    courierNumber: '',
                    senderPhone: '',
                    refuseReason: '',
                }
                this.formItem = newData
                //重置验证
                let form  = this.refs['form']
                form.resetFields()
                        this.modalVisible = false
                this.saving = false
            },
            handleSubmit () {
                let form  = this.refs['form']
                form.validate((valid) => {
                    if (valid) {
                        this.saving = true
                        if (this.formItem.orderRefundId) {
                        OrderRefund.update(this.formItem).then(res => {
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                                this.handleReset()
                             }
                            this.handleSearch()
                        }).finally(() => {this.saving = false})
                    } else {
                       OrderRefund.add(this.formItem).then(res => {
                            this.handleReset()
                            this.handleSearch()
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                            }
                          }).finally(() => {this.saving = false})
                        }
                   }
                })
            },
            handleSearch (page) {
                if (page) {
                    this.pageInfo.page = page
                }
                this.loading = true
                OrderRefund.list({page: this.pageInfo.page, limit: this.pageInfo.limit}).then(res => {
                    this.data = res.data.records
                    this.pageInfo.total = parseInt(res.data.total)
                }).finally(() => {this.loading = false})
            },
            handlePage (current) {
                this.pageInfo.page = current
                this.handleSearch()
            },
            handlePageSize (size) {
                this.pageInfo.limit = size
                this.handleSearch()
            },
            handleRemove (data) {
               let modal = this.$Modal
               modal.confirm({
                    title: '确定删除吗？',
                    onOk: () => {
                        OrderRefund.remove(data.orderRefundId).then(res => {
                            if (res.code === 0) {
                                this.pageInfo.page = 1;
                                this.$Message.success('删除成功');
                            }
                            this.handleSearch();
                        })
                    }
                })
            },
            handleClick (name, row) {
                switch (name) {
                    case 'remove':
                        this.handleRemove(row)
                        break
                }
            }
        },
        mounted: function () {
            this.handleSearch()
        }
    }
</script>
